﻿/////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Eric Bowen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Author's blog: http://blog.scrappydog.com
// Updated source code available:  http://scrappydb.codeplex.com
/////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Configuration;
using System.Diagnostics;

namespace ScrappyDB.Utilities
{
    public static class Configuration
    {
        private static string _runningconfig;
        public static string RunningConfig
        {
            get
            {
                _runningconfig = ConfigurationManager.AppSettings["RunningConfig"];

                if (string.IsNullOrEmpty(_runningconfig))
                    return string.Empty;

                return _runningconfig + "_";
            }
        }

        public static string AccessKeyId
        {
            get { return ConfigurationManager.AppSettings[RunningConfig + "AccessKeyID"]; }
        }

        public static string SecretAccessKey
        {
            get { return ConfigurationManager.AppSettings[RunningConfig + "SecretAccessKey"]; }
        }

        public static bool UseSsl
        {
            get
            {
                string useSsl = ConfigurationManager.AppSettings[RunningConfig + "UseSsl"];
                if (useSsl == null)
                    return true;

                return bool.Parse(useSsl);
            }
        }

        public static string ProxyHost
        {
            get { return ConfigurationManager.AppSettings[RunningConfig + "ProxyHost"]; }
        }

        public static string ProxyPort
        {
            get { return ConfigurationManager.AppSettings[RunningConfig + "ProxyPort"]; }
        }

        public static string ServiceURL
        {
            get { return ConfigurationManager.AppSettings[RunningConfig + "ServiceURL"]; }
        }

        public static DateTime CacheTimeout
        {

            get
            {
                int sec;
                if (int.TryParse(ConfigurationManager.AppSettings[RunningConfig + "CacheTimeoutSeconds"], out sec))
                    return DateTime.Now.ToUniversalTime().AddSeconds(sec);

                return DateTime.Now.ToUniversalTime().AddSeconds(30);
            }
        }

        public static TimeSpan CacheTimeoutTimeSpan
        {

            get
            {
                int sec;

                //default to 30 seconds
                if (!int.TryParse(ConfigurationManager.AppSettings[RunningConfig + "CacheTimeoutSeconds"], out sec))
                    return new TimeSpan(0, 0, 30);

                return new TimeSpan(0, 0, sec);
            }
        }

        /// <summary>
        /// Valid values: Off, Error, Warning, Info, Verbose
        /// </summary>
        private static string MinimumTraceLevel
        {
            get { return ConfigurationManager.AppSettings[RunningConfig + "TraceLevel"]; }
        }


        public static TraceSwitch DefaultTraceSwitch
        {
            get
            {
                TraceSwitch result = new TraceSwitch("General", "ScrappyDB Default");
                if (!string.IsNullOrEmpty(MinimumTraceLevel))
                {
                    result.Level = (TraceLevel) Enum.Parse(typeof (TraceLevel), MinimumTraceLevel);
                }
                else
                {
                    result.Level = TraceLevel.Verbose;
                }
                return result;
            }
        }
        

    }
}
